package dao.Impl;

import dao.BaseDao;
import dao.UserDao;
import entity.User;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {

    @Override
    public int register(String account, String nickName, String password, int genter, String idCardNo,String email, String phone) {
        String sql="insert into user(account,nickName,password,gender,idCardNo,email,phone) values(?,?,?,?,?,?,?)";
        Object params[]={account,nickName,password,genter,idCardNo,email,phone};
        int hangshu=executeUpdate(sql,params);
        return hangshu;
    }

    @Override
    public List<User> login(String Nickname, String password) {
        String sql="select account,nickName,password,gender,idCardNo,email,phone from user where Nickname=? and password=?";
        Object params[]={Nickname,password};
        User user=null;
        List<User> list=new ArrayList<>();
        ResultSet rs=executeQuery(sql,params);
        try {
            while (rs.next()){
                user=new User();
                user.setAccount(rs.getString("account"));
                user.setNickName(rs.getString("nickName"));
                user.setPassword(rs.getString("password"));
                user.setGender(rs.getInt("gender"));
                user.setIdCardNo(rs.getString("idCardNo"));
                user.setEmail(rs.getString("email"));
                user.setPhone(rs.getString("phone"));
                list.add(user);

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(connection,pstmt,rs);
        }

        return list;
    }
}
